package com.controller;


import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.entity.Book;
import com.service.BookService;


@Controller
public class BookController {

    @Autowired
    BookService bookService;

    @RequestMapping("/getbooks")
    public String books(Map<String, Object> map) {
        List<Book> books = bookService.getBooks();
        map.put("allBooks", books);
        return "allbooks";
    }

    @RequestMapping(value = "/insert", produces = "text/html;charset=UTF-8")
    public String insert() {
        return "insert";
    }

    @RequestMapping(value = "/insertBook")
    public String insertBook(Book book, MultipartFile book_pic) throws IllegalStateException, IOException {

        String originalFilename = book_pic.getOriginalFilename();
        if (originalFilename != null && originalFilename.length() > 0) {
            String pic_path = "D:\\develop\\niit\\workspace\\spring\\springFinal\\src\\main\\webapp\\img\\";
            String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
            File newFile = new File(pic_path + newFileName);
            book_pic.transferTo(newFile);
            book.setPic(newFileName);
        }
        bookService.insertBook(book);
        return "redirect:getbooks";
    }

    @RequestMapping(value = "/deleteBookById")
    public String deleteBookById(Integer id) {
        bookService.deleteBookById(id);
        return "redirect:getbooks";
    }

    @RequestMapping(value = "/findById")
    public String findById(Model model, Integer id) {
        Book book = bookService.findById(id);
        model.addAttribute("book", book);
        return "update";
    }

    @RequestMapping(value = "/updateBook")
    public String updateBook(Book book, MultipartFile book_pic) throws IllegalStateException, IOException {
        System.out.println(book_pic);
        if (book_pic != null) {
            String originalFilename = book_pic.getOriginalFilename();
            if (originalFilename != null && originalFilename.length() > 0) {
                String pic_path = "D:\\develop\\niit\\workspace\\spring\\springFinal\\src\\main\\webapp\\img\\";
                String newFileName = UUID.randomUUID() + originalFilename.substring(originalFilename.lastIndexOf("."));
                File newFile = new File(pic_path + newFileName);
                book_pic.transferTo(newFile);
                book.setPic(newFileName);
            }
        }
        bookService.updateBook(book);
        return "redirect:getbooks";
    }
    @RequestMapping(value = "/find",produces = "text/html;charset=UTF-8")
    public String find(Model model, Integer id) {
        Book book = bookService.findById(id);
        return "select";
    }
}